package com.ld.security.auth.server.modules.sys.api;

import com.ld.security.auth.common.base.constants.JwtTokenConstant;
import com.ld.security.auth.common.base.entity.JwtUserInfo;
import com.ld.security.auth.server.modules.sys.entity.SysToken;
import com.ld.security.auth.server.modules.sys.service.ISysTokenService;
import com.ld.security.common.core.base.po.EntitysPo;
import com.ld.security.common.core.base.vo.DataVo;
import com.ld.security.common.shiro.constants.ShiroCacheConstants;
import com.ld.security.common.tomcat.base.api.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.session.Session;
import org.apache.shiro.session.mgt.DefaultSessionKey;
import org.apache.shiro.session.mgt.SessionManager;
import org.apache.shiro.session.mgt.eis.SessionDAO;
import org.crazycake.shiro.RedisManager;
import org.crazycake.shiro.RedisSessionDAO;
import org.crazycake.shiro.SerializeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collection;
import java.util.Set;

@RestController
@Api(tags="令牌管理")
@RequestMapping("/api/sysToken")
public class SysTokenApi extends SuperApi<SysToken> {



    @Autowired
    SessionManager sessionManager;


    @Override
    public DataVo saveBatch(@RequestBody EntitysPo<SysToken> po) {
        for (SysToken sysToken : po.getList()) {
            try {
                if(sysToken.getTokenStatus() == JwtTokenConstant.tokenStatus.kickout){
                    Session session =  sessionManager.getSession (        new DefaultSessionKey(sysToken.getToken()));
                    session.setAttribute(ShiroCacheConstants.kickoutKey,true);

                }
            }catch (Exception e){

            }

        }
        return super.saveBatch(po);
    }


    @Autowired
    RedisSessionDAO sessionDAO;
    @Autowired
    RedisManager redisManager;

    @Autowired
    ISysTokenService iSysTokenService;

    @ApiOperation("同步会话为token")
    @RequestMapping(value = "/asyncSesssion",method = RequestMethod.POST)
    public DataVo asyncSesssion(){
    // 每次获取一个session 防止内存饱满
        Set<byte[]>keys =  redisManager.keys(sessionDAO.getKeyPrefix()+"*");
        for (byte[] key : keys) {
            Session session = (Session) SerializeUtils.deserialize( redisManager.get(key));
            String id =(String)session.getId();
            JwtUserInfo userInfo = (JwtUserInfo)session.getAttribute(ShiroCacheConstants.userKey);

            SysToken sysToken =  iSysTokenService.newInstance();
            sysToken.setId(id);
            sysToken.setToken(id);
            sysToken.setTokenStatus(JwtTokenConstant.tokenStatus.not_auth);
            sysToken.setLabel("ip:"+session.getHost());
            if(userInfo != null){
                sysToken.setUsername(userInfo.getUser().getUsername());
                sysToken.setTokenStatus(JwtTokenConstant.tokenStatus.login_auth);
            }
            iSysTokenService.save(sysToken);
        }
        return DataVo.ofMsg("同步会话："+keys.size()+"个");

    }



}
